app.controller('BusTaskListCtrl', function ($scope, ajax, $modal, toaster) {


	//任务状态
	$scope.statusList = [
		{ id: 1, name: "已创建" },
		{ id: 2, name: "已分配" },
		{ id: 3, name: "进行中" },
		{ id: 4, name: "已完成" },
		{ id: 5, name: "已取消" },
		//             { id: 6, name: "已失败" },
		//             { id: 7, name: "已中止" },
		//             { id: 8, name: "未执行" },
	];


	$scope.reload = $scope.page.loader('task/bs/{pageIndex}/{pageSize}/page');
	$scope.$on('pageIndex', function (event, p) { $scope.reload(p, $scope.queryMap); });
	$scope.reload(1);

	$scope.timeRange = {};
	$scope.queryMap = {};
	$scope.query = function () {
		if ($scope.statusList.selected != undefined) {
			$scope.queryMap.status = $scope.statusList.selected.id;
		} else if ($scope.statusList.selected == undefined) {
			delete $scope.queryMap.status;
		}
		if ($scope.timeRange.startTime && $scope.timeRange.endTime) {
			var starTime = $scope.timeRange.startTime.pattern("yyyy-MM-dd");
			var endTime = $scope.timeRange.endTime.pattern("yyyy-MM-dd");
			$scope.queryMap.createTime = starTime + "#@" + endTime;
		} else if ($scope.timeRange.startTime == null || $scope.timeRange.endTime == null) {
			delete $scope.queryMap.createTime;
		}
		$scope.reload(1, $scope.queryMap);
	};

	// 新增、更新
	$scope.write = function (size, current_data, submit_type) {
		$scope.current_data = angular.copy(current_data);
		$modal.open({
			templateUrl: 'template/bus/task/writeModal.html?v=4f824c2a74',
			controller: 'BusTaskModalCtrl',
			size: size,
			resolve: {
				param: function () {
					return {
						"data": $scope.current_data,
						"type": submit_type
					};
				}
			}
		}).result.then(function (param) {
			if (param.type == 'update') {
				$scope.page.mod(new Req.PUT({
					id:  param.data.id,
					fields: param.data,
					resource: 'task/bs/update'
				}), $scope.reload);
			}
		});
	};

	$scope.cancel = function (task) {
		$modal.open({
			templateUrl: 'template/confirmModal.html?v=82a28a704b',
			controller: 'ConfirmModalCtrl',
			size: 'sm',
			resolve: {
				param: function () {
					return {
						"title": '取消任务',
						"msg": '已有乘客且进行中的任务将无法取消，确定要取消当前任务?'
					};
				}
			}
		}).result.then(function () {
			ajax.call(new Req.POST({
				resource: 'task/bs/' + task.id + '/cancel'
			})).then(function (result) {
				if (result.code == 200) {
					$scope.reload($scope.bigCurrentPage, $scope.queryMap);
				} else if (result.code != 423) {
					toaster.pop("error", "失败", result.result);
				}
			}, $scope.serverError);
		});
	};
});

app.controller('ConfirmModalCtrl', function ($scope, $modalInstance, param) {
	$scope.param = param;
	$scope.ok = function () { $modalInstance.close(param); };
	$scope.cancel = function () { $modalInstance.dismiss('cancel'); };
});